package com.m4f.appengine.utils.security.authentication;

import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class GoogleAccountsAuthenticationEntryPoint implements AuthenticationEntryPoint {
	
	private static final Logger LOGGER = Logger.getLogger(GoogleAccountsAuthenticationEntryPoint.class.getName());
	
	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException arg2) throws IOException, ServletException {
		UserService userService = UserServiceFactory.getUserService();
		String loginPageRedirectURL = userService.createLoginURL(request.getRequestURI());
		LOGGER.info("**** EOOOE: " + loginPageRedirectURL);
	    response.sendRedirect(loginPageRedirectURL);
	    
	}

}
